package com.yangjian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yangjian.bean.Category;
import com.yangjian.bean.Dish;
import com.yangjian.bean.Setmeal;
import com.yangjian.dao.CategoryDao;
import com.yangjian.exception.CustomException;
import com.yangjian.page.PageParameter;
import com.yangjian.service.CategoryService;
import com.yangjian.service.DishService;
import com.yangjian.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl  implements CategoryService {


    @Autowired
    private CategoryDao dao;

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    /**
     * 添加分类
     * @param category //分类的实体类
     * @return
     */
    @Override
    public int add(Category category) {
        int row = dao.insert(category);

        return row;
    }

    /**
     * 分类分页查询
     *
     * @param pageParameter //分页信息
     * @return
     */
    @Override
    public IPage<Category> findPage(PageParameter pageParameter) {
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();

        IPage<Category> page =new Page<>(pageParameter.getPage(),pageParameter.getPageSize());

        IPage<Category> categoryIPage = dao.selectPage(page,lqw);
        return categoryIPage;
    }

    /**
     * 单个删除
     *
     * @param id
     * @return
     */
    @Override
    public int delete(Long id) {

        //先查询菜品表里是否有相关联的菜品
        List<Dish> list1 = dishService.findByCid(id);
        //判断dishCount是否有

        if (list1 !=null &&list1.size()>0){
            //return 0;

            //抛出一个异常
           throw  new CustomException("该菜品有相关联的菜品，禁止删除");
        }

        //先查询套餐表里是否有相关联的套餐
        List<Setmeal> list2  = setmealService.findSetmeal(id,1);
        //判断setmealCount是否有
        if (list2 !=null && list2.size()>0){
            //return 0;
            //抛出一个异常
            throw  new CustomException("该菜品有相关联的套餐，禁止删除");
        }

        //如果没有关联的菜品和套餐，就可以删除了
        int row = dao.deleteById(id);
        return row;
    }

    /**
     * 更新分类
     *
     * @param category
     * @return
     */
    @Override
    public int update(Category category) {

        int row = dao.updateById(category);
        return row;
    }

    /**
     * 根据type来查询分类
     *
     * @param type
     * @return
     */
    @Override
    public List<Category> findByte(Integer type) {
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        lqw.eq(type!=null,Category::getType ,type);

        List<Category> list = dao.selectList(lqw);


        return list;
    }

    /**
     * 根据id查询分类
     *
     * @param id
     * @return
     */
    @Override
    public Category findById(Long id) {
        Category category = dao.selectById(id);

        return category;
    }
}
